package com.qy33.select;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.qy33.entity.Line;
import com.qy33.entity.Line2;
import com.qy33.service.LineService;
import com.qy33.util.AmountUtil;


/**
 * 线段涨跌幅统计
 * @author Henry
 * @date 2013-1-25
 *
 */
public class LineRangeStat {
	
	private LineService lineService = new LineService();

	public void line2UpDownRange(String code) {
		List<Line2> line2s = lineService.getAllLine2(code, null);
		
		// 先找出所有 UpDown的双线
		List<Line2> base = new ArrayList<Line2>();
		for (Line2 line2 : line2s) {
			if (line2.getType() == Line2.Type_UpDown) {
				base.add(line2);
			}
		}
		
		int[] array = new int[10];
		int total = 0;
		for (Line2 line2 : base) {
			
			/*
			 * 计算公式:
			 * (2线起点 - 2线结束点) / (1线结束点 - 1线起点)
			 */
			Line l1 = line2.getBegin();
			Line l2 = line2.getEnd();
			
			BigDecimal fenzi = AmountUtil.subtract(l2.getBegin().getPrice(), l2.getEnd().getPrice());
			BigDecimal fenmu = AmountUtil.subtract(l1.getEnd().getPrice(), l1.getBegin().getPrice());
			
			BigDecimal range = AmountUtil.divide(fenzi, fenmu);
			if (range == null) {
				continue;
			}
			
			int pos = 0;
			System.out.println(range.doubleValue());
			
			if (range.doubleValue() < 0.0) {
				System.out.println(range.doubleValue());
			}
			
			if (range.doubleValue() < 1){
				pos = Integer.parseInt(range.toString().substring(2, 3));
			}
			array[pos]++;
			total++;
		}
		total = total - array[0];
		for (int i = 1; i < array.length; i++) {
			System.out.println(i +"共有" + array[i] +"个，占比" + (array[i]/(double)total)*100 + "%");
		}
	}
	
	public static void main(String[] args) {
//		String a = "0.123456";
//		System.out.println(a.substring(2, 3));
		
		LineRangeStat lineRangeStat = new LineRangeStat();
		lineRangeStat.line2UpDownRange("999999");
	}
	
}
